package com.freesky.biz.icbc.service.impl;

import com.alibaba.fastjson2.JSON;
import com.freesky.biz.icbc.domain.TIntegralExchange;
import com.freesky.biz.icbc.domain.TIntegralPartner;
import com.freesky.biz.icbc.domain.TIntegralPartnerRecord;
import com.freesky.biz.icbc.domain.TIntegralPartnerUser;
import com.freesky.biz.icbc.mapper.IntegralPartnerMapper;

import com.freesky.biz.icbc.service.IIntegralPartnerService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.domain.entity.SysUser;
import com.freesky.common.enums.ExportStatusEnum;

import com.freesky.common.utils.SecurityUtils;
import com.freesky.common.utils.StringUtils;
import com.freesky.system.domain.TExportRecord;
import com.freesky.system.service.ITExportRecordService;

import com.freesky.system.task.SyncReport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class IntegralPartnerServiceImpl implements IIntegralPartnerService {

    private static final Logger logger = LoggerFactory.getLogger(IntegralPartnerServiceImpl.class);

    @Autowired
    private IntegralPartnerMapper integralPartnerMapper;

    @Autowired
    private ITExportRecordService exportRecordService;

    @Autowired
    private SyncReport syncReport;


    @Override
    public List<TIntegralPartner> findList(TIntegralPartner entity) {
        return integralPartnerMapper.findList(entity);
    }

    @Override
    public AjaxResult exportFile(TIntegralPartner entity) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("INTEGRAL_PARTNER");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        System.out.println(JSON.toJSONString(exportRecord));
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(entity, new TIntegralPartner(), IIntegralPartnerService.class,
                "findList", "金币领取记录",Long.parseLong(exportRecord.getId()),sysUser.getUserName());

        return AjaxResult.success();
    }

    @Override
    public List<TIntegralPartnerUser> findUserList(TIntegralPartnerUser entity) {
        return integralPartnerMapper.findUserList(entity);
    }

    @Override
    public AjaxResult exportUserFile(TIntegralPartnerUser entity) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("INTEGRAL_PARTNER_USER");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        System.out.println(JSON.toJSONString(exportRecord));
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(entity, new TIntegralPartnerUser(), IIntegralPartnerService.class,
                "findUserList", "合伙人用户",Long.parseLong(exportRecord.getId()),sysUser.getUserName());

        return AjaxResult.success();
    }

    @Override
    public List<TIntegralPartnerRecord> findRecordList(TIntegralPartnerRecord entity) {
        return integralPartnerMapper.findRecordList(entity);
    }

    @Override
    public AjaxResult exportRecordFile(TIntegralPartnerRecord entity) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("INTEGRAL_PARTNER_RECORD");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        System.out.println(JSON.toJSONString(exportRecord));
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(entity, new TIntegralPartnerRecord(), IIntegralPartnerService.class,
                "findRecordList", "奖励记录",Long.parseLong(exportRecord.getId()),sysUser.getUserName());

        return AjaxResult.success();
    }

    @Override
    public List<TIntegralExchange> findPointList() {
        return integralPartnerMapper.findPointList();
    }

    @Override
    public int processIntegralExchange(TIntegralExchange integralExchange) {
        TIntegralPartnerUser integralPartnerUser = integralPartnerMapper.getOnePartnerUser(integralExchange.getMobile(),integralExchange.getOpenId());
        if(integralPartnerUser==null){
            logger.info(integralExchange.getMobile()+",未注册积分合伙人用户!");
            integralPartnerMapper.updatePointPartnerProcessed(integralExchange.getId(),"1");
            return 0;
        }
        //一级合伙人，奖励
        String partnerId = integralPartnerMapper.getBindPartnerUserById(integralPartnerUser.getPartnerId());
        if(StringUtils.isNotEmpty(partnerId)){
            logger.info(integralExchange.getMobile()+",一级合伙人："+partnerId+"");
            TIntegralPartnerRecord integralPartnerRecord = new TIntegralPartnerRecord();
            integralPartnerRecord.setId(SeqUtil.getSeqId());
            integralPartnerRecord.setPartnerId(partnerId);
            integralPartnerRecord.setBusiId(integralExchange.getId());
            integralPartnerRecord.setTotalAmt(integralExchange.getAmt());
            integralPartnerRecord.setAmt(String.valueOf(Integer.parseInt(integralExchange.getAmt())/100));
            integralPartnerRecord.setFlag("1");
            integralPartnerRecord.setBindPartnerId(integralPartnerUser.getPartnerId());
            integralPartnerMapper.addPartnerRecord(integralPartnerRecord);

            //二级合伙人，奖励0.5
            String partnerId2 = integralPartnerMapper.getBindPartnerUserById(partnerId);
            if(StringUtils.isNotEmpty(partnerId2)){
                logger.info(integralExchange.getMobile()+",二级合伙人："+partnerId2+"");
                TIntegralPartnerRecord integralPartnerRecord2 = new TIntegralPartnerRecord();
                integralPartnerRecord2.setId(SeqUtil.getSeqId());
                integralPartnerRecord2.setPartnerId(partnerId2);
                integralPartnerRecord2.setBusiId(integralExchange.getId());
                integralPartnerRecord2.setTotalAmt(integralExchange.getAmt());
                integralPartnerRecord2.setAmt(String.valueOf(Float.parseFloat(integralExchange.getAmt())/200.0));
                integralPartnerRecord2.setFlag("2");
                integralPartnerRecord2.setBindPartnerId(integralPartnerUser.getPartnerId());
                integralPartnerRecord2.setBindPartnerId1(partnerId);
                integralPartnerMapper.addPartnerRecord(integralPartnerRecord2);
            } else {
                logger.info(integralExchange.getMobile()+",未找到二级合伙人!");
            }
        } else {
            logger.info(integralExchange.getMobile()+",未找到一级合伙人!");
        }
        integralPartnerMapper.updatePointPartnerProcessed(integralExchange.getId(),"1");
        return 0;
    }

    @Override
    public int updatePartnerUserTotalAmt() {
        return integralPartnerMapper.updatePartnerUserTotalAmt();
    }
}
